package app.kotlin_1.utils

import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.util.Log

import java.util.ArrayList

/**
 * Created by 刘智强 on 2017/4/18.
 */

object XPermissionUtils {
    var PHONE = 0x00//电话
    var LOCATION = 0x01//位置
    var CAMERA = 0x02//相机
    var AUDIO = 0x04//语音
    var EXTERNAL = 0x08//存储
    var MORE = 0x10//多个
    private var mRequestCode = -1

    private var mOnPermissionListener: OnPermissionListener? = null
//    private var mRationaleHandler: RationaleHandler? = null

    interface OnPermissionListener {

        fun onPermissionGranted()

        fun onPermissionDenied(deniedPermissions: Array<String>)

        fun showRationale(permissions: Array<String>)

    }
//
//    abstract class RationaleHandler {
//        private var context: Context? = null
//        private var requestCode: Int = 0
//        private var permissions: Array<String>? = null
//        abstract fun showRationale(permissions: Array<String>)
//
//        internal fun showRationale(context: Context, requestCode: Int, permissions: Array<String>) {
//            this.context = context
//            this.requestCode = requestCode
//            this.permissions = permissions
//            showRationale(permissions)
//        }
//
//        @TargetApi(Build.VERSION_CODES.M)
//        fun requestPermissionsAgain() {
//            (context as Activity).requestPermissions(permissions!!, requestCode)
//        }
//    }


    @TargetApi(Build.VERSION_CODES.M)
    @JvmOverloads
    fun requestPermissions(context: Context, requestCode: Int, permissions: Array<String>, listener: OnPermissionListener) {
        if (context is Activity) {
            mRequestCode = requestCode
            mOnPermissionListener = listener
//            mRationaleHandler = handler
            val deniedPermissions = getDeniedPermissions(context, permissions)
            if (deniedPermissions.isNotEmpty()) {
                context.requestPermissions(deniedPermissions, requestCode)
            } else {
                if (mOnPermissionListener != null)
                    mOnPermissionListener!!.onPermissionGranted()
            }
        } else {
            throw RuntimeException("Context must be an Activity")
        }
    }

    /**
     * 请求权限结果，对应Activity中onRequestPermissionsResult()方法。
     */
    fun onRequestPermissionsResult(context: Activity, requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        if (mRequestCode != -1 && requestCode == mRequestCode) {
            if (mOnPermissionListener != null) {
                val deniedPermissions = getDeniedPermissions(context, permissions)
                if (deniedPermissions.isNotEmpty()) {
                    permissions.forEach {
                        if (shouldShowRequestPermissionRationale(context, it)) mOnPermissionListener!!.onPermissionDenied(deniedPermissions)
                        else mOnPermissionListener?.showRationale(deniedPermissions)
                    }
                } else {
                    mOnPermissionListener!!.onPermissionGranted()
                }

            }
        }
    }

    /**
     * 获取请求权限中需要授权的权限
     */
    private fun getDeniedPermissions(context: Context, permissions: Array<String>): Array<String> {
        val deniedPermissions = permissions.filter { ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_DENIED }
        return deniedPermissions.toTypedArray()
    }

    /**
     * 是否彻底拒绝了某项权限
     */
    fun hasAlwaysDeniedPermission(context: Context, vararg deniedPermissions: String): Boolean {
        return deniedPermissions.none { shouldShowRequestPermissionRationale(context, it) }
    }

    /**
     * 用户是否彻底禁止某一个权限
     */
    private fun shouldShowRequestPermissionRationale(context: Context, vararg deniedPermissions: String): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false
        var rationale: Boolean
        for (permission in deniedPermissions) {
            rationale = ActivityCompat.shouldShowRequestPermissionRationale(context as Activity, permission)
            if (rationale) return true
        }
        return false
    }
}
